package com.lsq.lwf.common.utils;

import java.io.File;
//import java.util.logging.Logger;

import javax.servlet.FilterConfig;

import org.apache.commons.lang3.StringUtils;

import com.lsq.lwf.common.exception.LightException;
import com.lsq.lwf.common.xmlparse.SqlXmlParse;
import com.lsq.lwf.common.xmlparse.ViewXmlParse;

public class Init {

	public Init(){
//		Logger log=Logger.getLogger(getClass().toString());
	}
	
	
	
	public Init( FilterConfig config){
		system_Init(config);
	}
	public static void system_Init(FilterConfig config){
		String config_addrs = config.getInitParameter("config-addr");
		if (StringUtils.isNotBlank(config_addrs)) {
			String[] config_addr = config_addrs.split(" ");
			if (config_addr != null && config_addr.length != 0) {
				for (String addr : config_addr) {
					String abPath = config.getServletContext().getRealPath(addr);
					File file = new File(abPath);
					if (file.exists()) {
						File[] listFile = file.listFiles();
						if (listFile.length > 0) {

							for (File f : listFile) {
								if (f.getName().indexOf("sql") > 0) {
									new SqlXmlParse(f);
								} else if (f.getName().indexOf("view") > 0) {
									new ViewXmlParse(f);
								} else {
									new LightException(f.getName()
											+ " is illegal");
								}
							}
						}
					}
				}
			}
		} else {
			new LightException("init param config-addr is not found");
		}
	}
}
